
<template>
  <div class="parent-card" v-infinite-scroll="load" :infinite-scroll-distance="10" :infinite-scroll-immediate="false">
    <el-image-viewer hide-on-click-modal @close="() => { showImage = false }" v-if="showImage"
      :url-list="[previewImage]" />
    <Modal v-model:show-modal="showModal">
      <video width="800" height="360" controls autoplay loop>
        <source :src="videoUrl" type="video/mp4" />
      </video>
    </Modal>
    <div class="right" v-show="userAuditList.length">
      <div class="album">
        <div class="album-cards">
          <div class="card" v-for="item, index in userAuditList">
            <div class="album-card" :style="{ 'background-image': `url('${item.resource[0].photo}')` }"
              v-if="item.type == 4" @click="preview(item.resource[0].photo)">
              <div class="btns" v-if="item.status == 0 || item.status == 1">
                <el-button class="reject"
                  @click.stop="reject(item.id, item.player_id, index, item.work_sheet_id, $event)">Reject</el-button>
                <el-button class="accept"
                  @click.stop="accept(item.id, item.player_id, index, item.work_sheet_id, $event)">Accept</el-button>
              </div>
            </div>
            <div class="album-card"
              :style="{ 'background-image': `url('${item.resource[0].video_cover ? item.resource[0].video_cover : image}')` }"
              v-else @click="watchVideo(item.resource[0].photo)">
              <div class="play">
                <img :src="playImage" />
              </div>
              <div class="btns" v-if="item.status == 0 || item.status == 1">
                <el-button class="reject"
                  @click.stop="reject(item.id, item.player_id, index, item.work_sheet_id, $event)">Reject</el-button>
                <el-button class="accept"
                  @click.stop="accept(item.id, item.player_id, index, item.work_sheet_id, $event)">Accept</el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <template class="empty-tip" v-show="!userAuditList.length">
      <div>No any data</div>
    </template>
  </div>
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue'
import { GetUserAlbum } from '@/vendors/userAlbum/userAlbum'
import { GetCertification } from '@/vendors/certification/certification'
const showImage = ref(false)
const showModal = ref(false)
const previewImage = ref('')
const videoUrl = ref('')
const playImage = new URL(`../images/play_icon.png`, import.meta.url).href
const image = new URL(`../images/video.png`, import.meta.url).href
import { ElLoading } from 'element-plus'
import { loadingService } from '@/utils/utils'
import useSetting from '@/vendors/store/setting'
const preview = (img: string) => {
  showImage.value = true
  previewImage.value = img
}
const {
  updateAudit,
} = GetCertification()
const {
  userAlbumStory,
  userAuditTotal,
  userAuditList,
  userAuditSize,
  userAuditPage
} = GetUserAlbum()
const setting = useSetting()
const reject = (id: number, player_id: number, loadIndex: number, work_sheet_id: number, event: any) => {
  const loading = ElLoading.service(loadingService)
  // var pro = event.currentTarget;   // 当前元素
  updateAudit(id, 3, player_id, work_sheet_id, (count: number) => {
    // pro.parentNode.style.display = "none";
    userAuditList.value.splice(loadIndex, 1)
    // 减少数量
    setting.setUserAlbumVideoCount(setting.count[2].num - 1)
    loading.close()
  })
}
const accept = (id: number, player_id: number, loadIndex: number, work_sheet_id: number, event: any) => {
  const loading = ElLoading.service(loadingService)
  // var pro = event.currentTarget;   // 当前元素
  updateAudit(id, 2, player_id, work_sheet_id, (count: number) => {
    // pro.parentNode.style.display = "none";
    userAuditList.value.splice(loadIndex, 1)
    console.log(userAuditList.value)
    // 减少数量
    const setting = useSetting()
    setting.setUserAlbumVideoCount(setting.count[2].num - 1)
    loading.close()
  })
}
userAlbumStory()
const load = () => {
  if (userAuditList.value.length == userAuditTotal.value) return
  userAlbumStory()
}
const watchVideo = (url: string) => {
  console.log(url)
  showModal.value = true
  videoUrl.value = url
}
watch(() => setting.refreshCount, () => {
  if (setting.refreshCount) {
    // 需要刷新当前页
    userAuditTotal.value = -1
    const len = userAuditList.value.length
    const flag = len != 0 && len % userAuditSize.value == 0  // 列表是整10倍数
    let index = 0
    let count = 0
    if (!flag) {
      // 非整10倍数
      userAuditPage.value -= 1
      index = Math.floor(len / userAuditSize.value) * userAuditSize.value
      count = len - index
      // userAuditList.value.splice(index, len) // 删除末尾元素
    }
    userAlbumStory(() => {
      setting.setRefreshCount(!setting.refreshCount)
      count && userAuditList.value.splice(index, count)
    })
  }
})
</script>
<style scoped lang="scss">
.parent-card {
  border-radius: 16px;
  background: white;
  display: flex;
  font-size: 16px;
  overflow-y: scroll;
  box-shadow: -3px 0 4px 1px #0000001A, 3px 0 4px 1px #0000001A;

  .right {
    max-height: calc(100vh - 330px);

  }
}

.right {
  margin: 24px 0;

}

.album-card {
  width: 250px;
  height: 368px;
  border-radius: 16px;
  position: relative;
  background-size: 100% 100%;
  cursor: pointer;
  background-size: cover;
  background-position: center;
}

.album-cards {
  display: flex;
  flex-wrap: wrap;
}

.btns {
  width: 250px;
  display: flex;
  justify-content: center;
  position: absolute;
  bottom: 16px;
  left: 0;
  right: 0;
  margin: 0 auto;
}

.reject,
.accept {
  border: 0;
  width: 102px;
  height: 48px;
  border-radius: 12px;
  font-family: Montserrat;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0em;
}

.reject {
  color: rgba(250, 74, 130, 1);
}

.accept {
  color: white;
  background: rgba(0, 132, 255, 1);
}

.card {
  display: flex;
  border-radius: 16px;
  margin-left: 48px;
  margin-bottom: 50px;
}


.play {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(white, .5);
  display: flex;
  justify-content: center;
  flex-direction: column;
  cursor: pointer;

  img {
    width: 37px;
    height: 37px;
    margin: 0 auto;
  }
}
</style>